<?php

namespace app\common\command;

use app\common\service\CodexPriceService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class SyncCodePrice extends Command
{
    protected function configure()
    {
        $this->setName('sync_code_price')
            ->setDescription('同步codex价格');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln("✅ 同步codex价格！");
        $now = time();
        $date = date('Y-m-d');
        // 计算当天 00:00:00 和 23:59:59 的时间戳
        $start = strtotime($date);
        $end   = strtotime($date . ' +1 day') - 1; // 精确到 23:59:59
        $info = Db::name('codex_price')->whereBetween('create_time', [$start, $end])->find();
        if($info){
            $output->writeln("日期{$date}价格已存在");
            return;
        }
        $service = app()->make(CodexPriceService::class);
        $newPrice = $service->getNewPrice();
        $codexPrice = $newPrice * 1.01;
        $res = $service->create([
            'symbol' => 'CODEX',
            'price'  => $codexPrice,
            'create_time' => $now,
        ]);
        $output->writeln(json_encode($res, JSON_UNESCAPED_UNICODE));
    }
}